package com.bba.manager.sku.controller;

import com.bba.manager.common.model.sku.Car;
import com.bba.manager.common.model.sku.CarVendor;
import com.bba.manager.common.util.ResponseVO;
import com.bba.manager.sku.service.impl.CarVendorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 厂商汽车库存 前端控制器
 * </p>
 *
 * @author SYANDZZY
 * @since 2025-08-12
 */
@RestController
@RequestMapping("/carVendor")
public class CarVendorController {
    @Autowired
    private CarVendorServiceImpl carVendorService;
    @GetMapping("/getByCarId")
    public ResponseVO getByCarId(@RequestParam Long carId){
        return ResponseVO.success(carVendorService.getByCarId(carId));
    }

    @PostMapping("/decount/{id}/{num}")
    public ResponseVO decount(
            @PathVariable("id") Long id,
            @PathVariable("num") Integer num
    ){
        CarVendor car = carVendorService.getByCarId(id);
        if(car.getRepertory() < num){
            return ResponseVO.fail();
        }
        car.setRepertory(car.getRepertory()-num);
        carVendorService.updateById(car);
        return ResponseVO.success();
    }
    @GetMapping("/update")
    public ResponseVO updateCar(@RequestBody CarVendor carVendor) {
        carVendorService.updateById(carVendor);
        return ResponseVO.success();
    }

    @DeleteMapping("/delete")
    public ResponseVO delete(@RequestParam Long id) {
        carVendorService.removeById(id);
        return ResponseVO.success();
    }

    @PostMapping("/add")
    public ResponseVO add(CarVendor car) {
        carVendorService.save(car);
        return ResponseVO.success();
    }
    @GetMapping("/getone")
    public ResponseVO getone(@RequestParam Long id){
        CarVendor byId = carVendorService.getById(id);
        return ResponseVO.success(byId);
    }

    @GetMapping("/getall")
    public ResponseVO getall() {
        return ResponseVO.success(carVendorService.list());
    }
    @GetMapping("/getCarVendor")
    public ResponseVO getCarVendor(CarVendor car) {
        return ResponseVO.success(carVendorService.getCarVendor(car));
   }
}
